package com.dragon.controller;

import com.dragon.pojo.Result;
import com.dragon.pojo.Training.TrainingInfo;
import com.dragon.pojo.Training.TrainingSelectInfo;
import com.dragon.service.TrainingService;
import com.dragon.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("training")
public class TrainingController {

    @Autowired
    TrainingService trainingService;

    /**
     * 查询题单列表
     * @param request 获取 token
     * @param info TrainingSelectInfo
     * @return 返回响应
     */
    @GetMapping("list")
    public Result GetTrainingList(HttpServletRequest request, TrainingSelectInfo info) {
        String jwt = request.getHeader("Accept-token");
        if(jwt !=null && !jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                info.setUserId((Integer) claims.get("id"));
            }
        }
        return Result.success(trainingService.getTrainingList(info));
    }

    /**
     * 查询题单信息
     * @param request 获取 token
     * @param trainingId 题单的Id
     * @return 返回响应
     */
    @GetMapping("info/{trainingId}")
    public Result GetTrainingInfo(HttpServletRequest request, @PathVariable Integer trainingId) {
        String jwt = request.getHeader("Accept-token");
        Integer userId = null;
        if(jwt !=null && !jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                userId = (Integer) claims.get("id");
            }
        }
        return Result.success(trainingService.getTrainingInfo(trainingId, userId));
    }

    /**
     * 查询题单题目
     * @param request 获取 token
     * @param trainingId 题单的 ic
     * @return 返回响应
     */
    @GetMapping("problem")
    public Result GetTrainingProblem(HttpServletRequest request, Integer trainingId) {
        String jwt = request.getHeader("Accept-token");
        Integer userId = null;
        if(!jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                userId = (Integer) claims.get("id");
            }
        }
        return Result.success(trainingService.getTrainingProblemList(trainingId, userId));
    }

    /**
     * 创建题单
     * @param request 获取 token
     * @param info TrainingAddInfo
     * @return 返回响应
     */
    @PostMapping("add")
    public Result CreateTraining(HttpServletRequest request, @RequestBody TrainingInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行新增:\"{}\" 题单操作", claims.get("id"), info.getTrainingName());
        if(claims.get("permissions").equals("admin")){
            info.setCreator((String) claims.get("nickname"));
            trainingService.addTraining(info);
            return Result.success("创建成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 删除题单
     * @param request 获取 token
     * @param trainingId 题单 id
     * @return 返回响应
     */
    @DeleteMapping("del")
    public Result DeleteTraining(HttpServletRequest request, Integer trainingId) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行删除:{} 题单操作", claims.get("id"), trainingId);
        if(claims.get("permissions").equals("admin")){
            trainingService.deleteTraining(trainingId);
            return Result.success("删除成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 修改题单
     * @param request 修改 token
     * @param info TrainingInfo
     * @return 返回响应
     */
    @PutMapping("update")
    public Result UpdateTraining(HttpServletRequest request, @RequestBody TrainingInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行修改:{} 题单操作", claims.get("id"), info.getTrainingId());
        if(claims.get("permissions").equals("admin")){
            trainingService.updateTraining(info);
            return Result.success("修改成功");

        }
        return Result.error("没有权限",403);
    }
}
